<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
#div1{
	width:100px;
	height:100px;
	background-color:red;
	position:absolute;
	left:0px;
	top:50px;
}
#div2{
	height:300px;
	width:1px;
	background-color:black;
	position:absolute;
	left:300px;
}
</style>
<script>
function startMove(){
	var oDiv=document.getElementById('div1');
	
	//开一个定时器
	setInterval(function(){
		
		var speed=(300-oDiv.offsetLeft)/12;
		
		//var speed=Math.ceil(speed);
		//var speed=Math.floor(speed);
		speed>0?speed=Math.ceil(speed):speed=Math.floor(speed);
		/*
		if(speed>0)
		{
			speed=Math.ceil(speed);
		}
		else
		{
			speed=Math.floor(speed);
		}
		*/
		//300减去当前oDiv离左边的距离,不精确
		oDiv.style.left=oDiv.offsetLeft+speed+'px';
		//alert(speed);
		document.title=oDiv.offsetLeft+','+speed;
		//oDiv.style.left=oDiv.offsetLeft+speed+'px';
		//oDiv.style.left=
	},30);
};
</script>
</head>

<body>
<input type="button" value="开始运动" onclick="startMove()"/>
<div id="div1">
</div>
<div id="div2">
</div>
</body>
</html>
